<?php
namespace app\back\model;



use app\common\model\NodeTemplateComm;

class NodeTemplateModel extends NodeTemplateComm
{
    ///列表页数据
    public function getIndexDataList(){
        return $this->alias('a')->join('dh_config b','a.config_id = b.id','LEFT')->field('a.id,a.node_title,a.node_url,a.use_template_path,a.update_time,b.id as b_config_id,b.config_title')->select();
    }

    ///根据id获取数据
    public function getDataById($n_id = 0){
        return $this->where('id',$n_id)->field('id,node_title,node_url,config_id,use_template_path')->find();
    }

    ///修改当前使用模板
    public function addNewTemplateData($node_title,$node_url,$template_config_id,$template_config_path){
        return $this->save([
            'node_title'  => $node_title,
            'node_url' => $node_url,
            'config_id' => $template_config_id,
            'use_template_path' => $template_config_path,
            'update_time' => date('Y-m-d H:i:s'),
        ]);
    }

    ///修改当前使用模板
    public function saveUseTemplate($node_title,$node_url,$template_config_id,$template_config_path,$node_template_id){
        return $this->save([
            'node_title'  => $node_title,
            'node_url' => $node_url,
            'config_id' => $template_config_id,
            'use_template_path' => $template_config_path,
            'update_time' => date('Y-m-d H:i:s'),
        ],['id' => $node_template_id]);
    }

    ///新增时检测提交的url是否已经存在
    public function addCheckUrlItExist($node_url){
        $mp = array(
            'node_url'=>$node_url
        );
        return $this->where($mp)->count();
    }

    ///编辑时检测提交的url是否已经存在
    public function editCheckUrlItExist($node_url,$id){
        $mp = array(
            'node_url'=>$node_url,
            'id'=>['neq',$id],
        );
        return $this->where($mp)->count();
    }


    ///重置模板配置文件
    public function resetTemplateFile(){
        $template_config_path = CONF_PATH.'/extra/template.php';
        if(!file_exists($template_config_path)){throw new \Exception('请检查模板配置文件');}
        file_put_contents($template_config_path,"<?php ".PHP_EOL.PHP_EOL." return ".var_export(collection($this->where(['node_url'=>['neq',''],'use_template_path'=>['neq','']])->column('node_url,use_template_path'))->toArray(),true).';');
    }



}